package com.cskaoyan.aspect.wx_user;

import com.cskaoyan.bean.wx_resp.WxRespVo;
import com.cskaoyan.bean.wx_user.exception.UserNoLoginException;
import com.cskaoyan.config.AnonProperties;
import com.cskaoyan.config.UserLoginProperties;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liXiaoFei
 * @date 2021/10/23 15:04
 */
@Aspect
@Controller
@EnableConfigurationProperties(UserLoginProperties.class)
public class WxUserLoginAspect {

    UserLoginProperties userLoginProperties;

    public WxUserLoginAspect(UserLoginProperties userLoginProperties) {
        this.userLoginProperties = userLoginProperties;
    }


    @Pointcut("execution(* com.cskaoyan.controller.wx_*..*(..))")
    public void userLogin(){}

    @Before("userLogin()")
    public void userNoLogin(JoinPoint joinPoint) throws Throwable {
        Object target = joinPoint.getTarget();
        Class<?> aClass = target.getClass();
        RequestMapping requestMappingAnno = aClass.getAnnotation(RequestMapping.class);
        String[] value = new String[0];
        if (requestMappingAnno != null){
            value = requestMappingAnno.value();
        }
        StringBuilder sb = new StringBuilder();
        for (String ele : value) {
            sb.append(ele);
        }
        String url1 = sb.toString();
        Method[] methods = aClass.getDeclaredMethods();
        ArrayList<String> url = new ArrayList<>();
        for (Method method : methods) {
            GetMapping getMapping = method.getAnnotation(GetMapping.class);
            PostMapping postMapping = method.getAnnotation(PostMapping.class);
            RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
            String[] getMappingUrl = new String[0];
            String[] postMappingUrl = new String[0];
            String[] requestMappingUrl = new String[0];
            if (getMapping != null){
                getMappingUrl = getMapping.value();
            }
            if (postMapping != null){
                postMappingUrl = postMapping.value();
            }
            if (requestMapping != null){
                requestMappingUrl = requestMapping.value();
            }

            for (String url2 : getMappingUrl) {
                url.add("/" + url1 + "/" + url2);
            }
            for (String url2 : postMappingUrl) {
                url.add("/" + url1 + "/" + url2);
            }
            for (String url2 : requestMappingUrl) {
                url.add("/" + url1 + "/" + url2);
            }
        }

        List<String> loginUrl = userLoginProperties.getLoginUrl();
        for (String userLoginUrlProperties : loginUrl) {
            for (String userLoginUrl : url) {
                if (userLoginUrl.equals(userLoginUrlProperties)) {
                    if (SecurityUtils.getSubject().getPrincipals() == null) {
                        throw new UserNoLoginException();
                    }
                }
            }
        }

    }


}
